package kr.hkit.powerjava.chapter14.work;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.SwingConstants;
import java.awt.SystemColor;

public class KMConversion extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel panel_1;
	private JPanel panel_2;
	private ResultField panel_3;
	private InputPanel panel;
	private JButton btnConverter;
	private JLabel KMlabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					KMConversion frame = new KMConversion();
					frame.setVisible(true);
					frame.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public KMConversion() {
		initialize();
	}
	private void initialize() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(KMConversion.class.getResource("/com/sun/java/swing/plaf/motif/icons/DesktopIcon.gif")));
		setTitle("마일을 킬로미터로 변환");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 354, 120);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.activeCaption);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(0, 1, 0, 0));
		
		panel_1 = new JPanel();
		panel_1.setBackground(SystemColor.inactiveCaption);
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		flowLayout.setHgap(1);
		contentPane.add(panel_1);
		
		KMlabel = new JLabel("     거리를 마일 단위로");
		KMlabel.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(KMlabel);
		
		panel = new InputPanel();
		panel.setBackground(SystemColor.inactiveCaption);
		panel_1.add(panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		panel_2 = new JPanel();
		panel_2.setBackground(SystemColor.inactiveCaption);
		FlowLayout flowLayout_1 = (FlowLayout) panel_2.getLayout();
		flowLayout_1.setVgap(10);
		contentPane.add(panel_2);
		
		btnConverter = new JButton("변환");
		btnConverter.addActionListener(this);
		btnConverter.setBackground(Color.ORANGE);
		panel_2.add(btnConverter);
		
		panel_3 = new ResultField();
		panel_3.setBackground(SystemColor.inactiveCaption);
		FlowLayout flowLayout_2 = (FlowLayout) panel_3.getLayout();
		flowLayout_2.setVgap(10);
		contentPane.add(panel_3);
		
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		double kilo = 0.0;
		if(e.getActionCommand().equals("변환")) {
			try {
			kilo = Double.parseDouble(panel.getInputField().trim()) * 1.6;
			panel_3.setTextField(panel.getInputField().trim()+" 마일은 "
					+kilo+" 킬로미터 입니다.");
			} catch(NumberFormatException err) {
				JOptionPane.showMessageDialog(this, "숫자만 가능합니다.");
				panel.setInputField("");
			}
		}
		
		
	}
}
